//
//  SXTLoginView.m
//  8.18项目
//
//  Created by ma c on 16/8/21.
//  Copyright © 2016年 zhang. All rights reserved.
//

#import "SXTLoginView.h"
#import <UIKit/UIKit.h>

@interface SXTLoginView ()<UITextFieldDelegate>
@property (nonatomic ,strong) UITextField * userPhoneText;
@property (nonatomic ,strong) UITextField * passwordText;
@property (nonatomic ,strong) UIButton * loginViewBtn;
@property (nonatomic ,strong) UIButton * goLoginbtn;
@property (nonatomic ,strong) UILabel * whiteBacklabe;
@property (nonatomic ,strong) UILabel * grayLineLabel;

@end

@implementation SXTLoginView

-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.whiteBacklabe];
        [self addSubview:self.userPhoneText];
        [self addSubview:self.passwordText];
        [self addSubview:self.grayLineLabel];
        [self addSubview:self.loginViewBtn];
        [self addSubview:self.goLoginbtn];
    }
    return self;
}

//布局
-(void)layoutSubviews{
    
    [super layoutSubviews];
    __weak typeof(self) weakSelf = self;
    
    [_whiteBacklabe mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.mas_top).offset(15);
        make.left.equalTo(weakSelf.mas_left).offset(-1);
        make.right.equalTo(weakSelf.mas_right).offset(1);
        make.height.equalTo(@89);
    }];
    [_userPhoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.top.equalTo(weakSelf.whiteBacklabe.mas_top);
        make.size.mas_equalTo(CGSizeMake(VIEW_WIDTH, 44));
    }];
    [_passwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.top.equalTo(weakSelf.userPhoneText.mas_bottom).offset(5);
        make.size.mas_equalTo(CGSizeMake(VIEW_WIDTH, 44));
    }];
    
    [_grayLineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@1);
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.right.equalTo(weakSelf.mas_right).offset(-15);
        make.top.equalTo(weakSelf.userPhoneText.mas_bottom);
    }];
    [_loginViewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.whiteBacklabe.mas_bottom).offset(25);
        make.left.equalTo(weakSelf.mas_left).offset(16);
        make.height.equalTo(@35);
        make.right.equalTo(weakSelf.mas_right).offset(-15);
    }];
    [_goLoginbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.loginViewBtn.mas_bottom).offset(23);
        make.right.equalTo(weakSelf.mas_right).offset(-16);
        make.size.mas_equalTo(CGSizeMake(70, 30));
    }];
    
}


#pragma mark - 初始化

- (UITextField *)userPhoneText{
    if (!_userPhoneText) {
        _userPhoneText = [[UITextField alloc]init];
        _userPhoneText.delegate = self;
        _userPhoneText.placeholder = @"请输入登录账号";
        _userPhoneText.keyboardType = UIKeyboardTypeNumberPad;
        [_userPhoneText addTarget:self action:@selector(userTextField:) forControlEvents:(UIControlEventAllEditingEvents)];
    }
    return _userPhoneText;
}

- (UITextField *)passwordText{
    if (!_passwordText) {
        _passwordText = [[UITextField alloc]init];
        _passwordText.delegate = self;
        _passwordText.placeholder = @"请输入密码";
        [_passwordText addTarget:self action:@selector(passwordTextChangeText:) forControlEvents:(UIControlEventAllEditingEvents)];
    }
    return _passwordText;
}

-(UILabel *)whiteBacklabe{
    if (!_whiteBacklabe) {
        _whiteBacklabe = [[UILabel alloc]init];
        _whiteBacklabe.backgroundColor = [UIColor whiteColor];
        _whiteBacklabe.layer.borderColor = RGB(188, 186, 192).CGColor;
        _whiteBacklabe.layer.borderWidth = 1.0;
    }
    return _whiteBacklabe;
}

-(UILabel *)grayLineLabel{
    
    if (!_grayLineLabel) {
        _grayLineLabel = [[UILabel alloc]init];
        _grayLineLabel.backgroundColor = RGB(188, 186, 192);
    }
    return _grayLineLabel;
}

-(UIButton *)loginViewBtn{
    if (!_loginViewBtn) {
        _loginViewBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_loginViewBtn setTitle:@"登录" forState:(UIControlStateNormal)];
        _loginViewBtn.backgroundColor = RGB(229, 229, 229);
        _loginViewBtn.userInteractionEnabled = NO;
        _loginViewBtn.layer.cornerRadius = 5.0;
        [_loginViewBtn setTitleColor:RGB(132, 132, 132) forState:(UIControlStateNormal)];
        [_loginViewBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
        _loginViewBtn.selected = NO;
        [_loginViewBtn addTarget:self action:@selector(goNextViewController) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginViewBtn;
}

-(UIButton *)goLoginbtn{
    
    if (!_goLoginbtn) {
        _goLoginbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_goLoginbtn setTitle:@"去注册" forState:(UIControlStateNormal)];
        [_goLoginbtn setTitleColor:RGB(56, 145, 241) forState:(UIControlStateNormal)];
        _goLoginbtn.backgroundColor = MainColor;
    }
    return _goLoginbtn;
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == self.userPhoneText && range.location > 10) {
        return NO;
    }
    return YES;
    
}

//用户名修改内容时调用的方法
- (void)userTextField:(UITextField *)textfield{
    
    if (textfield.text.length == 11 && _passwordText.text.length > 5) {
        _loginViewBtn.backgroundColor = RGB(56, 166, 241);
        _loginViewBtn.userInteractionEnabled = YES;
        _loginViewBtn.selected = YES;
    }else{
        _loginViewBtn.backgroundColor = RGB(229, 229, 229);
        _loginViewBtn.userInteractionEnabled = NO;
        _loginViewBtn.selected = NO;
    }
}
//密码输入框修改内容是调用的方法
- (void)passwordTextChangeText:(UITextField *)textfield{
    
    if (textfield.text.length > 5 && _userPhoneText.text.length == 11) {
        _loginViewBtn.backgroundColor = RGB(56, 166, 241);
        _loginViewBtn.userInteractionEnabled = YES;
    }else{
        _loginViewBtn.backgroundColor = RGB(229, 229, 229);
        _loginViewBtn.userInteractionEnabled = NO;
    }
}

- (void)goNextViewController{
    if(_goLoginBlock){
        _goLoginBlock(@{@"LoginName":_userPhoneText.text,@"Lpassword":_passwordText.text});
    }
}
@end
